package cn.iocoder.yudao.module.distm.controller.admin.loss;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.distm.controller.admin.loss.vo.*;
import cn.iocoder.yudao.module.distm.dal.dataobject.loss.MealLossRecordDO;
import cn.iocoder.yudao.module.distm.service.loss.MealLossRecordService;

@Tag(name = "管理后台 - 配送管理-餐食损耗记录")
@RestController
@RequestMapping("/distm/loss")
@Validated
public class MealLossRecordController {

    @Resource
    private MealLossRecordService mealLossRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建配送管理-餐食损耗记录")
    @PreAuthorize("@ss.hasPermission('distm:loss:create')")
    public CommonResult<Long> createMealLossRecord(@Valid @RequestBody MealLossRecordSaveReqVO createReqVO) {
        return success(mealLossRecordService.createMealLossRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新配送管理-餐食损耗记录")
    @PreAuthorize("@ss.hasPermission('distm:loss:update')")
    public CommonResult<Boolean> updateMealLossRecord(@Valid @RequestBody MealLossRecordSaveReqVO updateReqVO) {
        mealLossRecordService.updateMealLossRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除配送管理-餐食损耗记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('distm:loss:delete')")
    public CommonResult<Boolean> deleteMealLossRecord(@RequestParam("id") Long id) {
        mealLossRecordService.deleteMealLossRecord(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除配送管理-餐食损耗记录")
                @PreAuthorize("@ss.hasPermission('distm:loss:delete')")
    public CommonResult<Boolean> deleteMealLossRecordList(@RequestParam("ids") List<Long> ids) {
        mealLossRecordService.deleteMealLossRecordListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得配送管理-餐食损耗记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('distm:loss:query')")
    public CommonResult<MealLossRecordRespVO> getMealLossRecord(@RequestParam("id") Long id) {
        MealLossRecordDO mealLossRecord = mealLossRecordService.getMealLossRecord(id);
        return success(BeanUtils.toBean(mealLossRecord, MealLossRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得配送管理-餐食损耗记录分页")
    @PreAuthorize("@ss.hasPermission('distm:loss:query')")
    public CommonResult<PageResult<MealLossRecordRespVO>> getMealLossRecordPage(@Valid MealLossRecordPageReqVO pageReqVO) {
        PageResult<MealLossRecordDO> pageResult = mealLossRecordService.getMealLossRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MealLossRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出配送管理-餐食损耗记录 Excel")
    @PreAuthorize("@ss.hasPermission('distm:loss:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMealLossRecordExcel(@Valid MealLossRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MealLossRecordDO> list = mealLossRecordService.getMealLossRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "配送管理-餐食损耗记录.xls", "数据", MealLossRecordRespVO.class,
                        BeanUtils.toBean(list, MealLossRecordRespVO.class));
    }

}